package com.cssy.xxkj.logic.web;

import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.dao.LogicWarehouseBatchInItemDAO;
import com.cssy.xxkj.logic.entity.LogicWarehouseBatchInDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseBatchInItemDO;
import com.cssy.xxkj.logic.service.ILogicWarehouseBatchInService;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/logic/batchIn")
public class BatchInController extends BaseController<LogicWarehouseBatchInDO>{

	private static String BATCHIN="/xxkj/logic/batchIn/batchInManager";

	private static String BATCHINITEM="/xxkj/logic/batchIn/batchInOperation";

	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;

	@Resource(name="oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;

	@Resource(name = "logicWarehouseBatchInService")
	private ILogicWarehouseBatchInService logicWarehouseBatchInService;

	@Resource(name = "logicWarehouseBatchInItemDAO")
	private LogicWarehouseBatchInItemDAO logicWarehouseBatchInItemDAO;

	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return BATCHIN;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs=this.oftenMothedUtil.getNoFinishConditions(scs, "zd010Stauts",BillStatusUtil.BATCH_IN_TWO);
		Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"zd003WhCode");
		scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		if(StringUtils.isBlank(scs.getOrderBy())){
			scs.setOrderBy("bizCreatorDate desc");
		}
		SearchResult result= this.logicWarehouseBatchInService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		model.addAttribute("username", SessionUtil.getUser(request).getRealName());
		if(!OperationFlag.ADD.equals(flag)){
			LogicWarehouseBatchInDO batchIn= this.logicWarehouseBatchInService.findListById(id);
			model.addAttribute("bi", batchIn);
		}
		return BATCHINITEM;
	}

	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			Map<String, String> paramMap= new HashMap<String, String>();
			paramMap.put("zd001BatchId", id);
			List<LogicWarehouseBatchInItemDO> itemList = this.logicWarehouseBatchInItemDAO.findLogicWarehouseBatchInItemByMap(paramMap);
			OutJsonGrid out = new OutJsonGrid(itemList);
			out.printJSon(response);
		}
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWarehouseBatchInDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		String item=request.getParameter("addressInfos");
		if(StringUtils.isEmpty(item)){
			bodyMap.isFail();
			bodyMap.configWebInfor("物料不能为空！");
			return bodyMap.modelMap;
		}
		try {
			this.logicWarehouseBatchInService.batchInSave(curAP, request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (SyBaseException e) {
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				LogicWarehouseBatchInDO lot=this.logicWarehouseBatchInService.findListById(field);
				if(lot.getId()!=null){
					lot.setRemoved(1l);
					this.logicWarehouseBatchInService.update(lot, SessionUtil.getUser(request));
				}
			}

			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

}
